<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>
  
  <script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

const animationDuration = 6000;

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "300px"
    }
  },
  data() {
    return {
      chart: null
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");

      this.chart.setOption({
        tooltip: {
          trigger: "axis",
          show: false,
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "shadow" // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        // backgroundColor: "#00043A",
        legend: {
          show: false
        },
        grid: {
          left: "3%",
          right: "10%",
          bottom: "3%",
          containLabel: true
        },
        xAxis: [
          {
            splitLine: {
              show: false
            },
            type: "value",
            show: false
          }
        ],
        yAxis: [
          {
            splitLine: {
              show: false
            },
            axisLine: {
              //y轴
              show: false
            },
            type: "category",
            axisTick: {
              show: false
            },
            inverse: true,
            data: [
              "普通渣土",
              "沙石",
              "建筑材料",
              "盾构土",
              "拆除垃圾",
              "装潢垃圾"
            ],
            axisLabel: {
              color: "#A7D6F4",
              fontSize: 14
            }
          }
        ],
        series: [
          {
            name: "出土垃圾类型",
            type: "bar",
            barWidth: 12, // 柱子宽度
            label: {
              show: true,
              position: "right", // 位置
              color: "#A7D6F4",
              fontSize: 14,
              distance: 15, // 距离
              formatter: "{c} " // 这里是数据展示的时候显示的数据
            }, // 柱子上方的数值
            itemStyle: {
              barBorderRadius: [0, 20, 20, 0], // 圆角（左上、右上、右下、左下）

              color: new echarts.graphic.LinearGradient(
                1,
                0,
                0,
                0,
                [
                  {
                    offset: 0,
                    color: "#51C5FD"
                  },
                  {
                    offset: 1,
                    color: "#005BB1"
                  }
                ],
                false
              ) // 渐变
            },
            data: [40, 38, 32, 30, 28, 26]
          }
        ]
      });
    }
  }
};
</script>
  